package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;
    //添加收货地址
    @Override
    public void saveAddress(Address address) {

         address.setIsDefault(0);
         address.setUserId(UserHolder.get().getId());
         address.setCreateUser(UserHolder.get().getId());
         address.setUpdateUser(UserHolder.get().getId());
         addressMapper.insert(address);

    }
    //查询所有收货地址
    @Override
    public List<Address> findList() {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId,UserHolder.get().getId());



        return addressMapper.selectList(wrapper);
    }

    //修改收货地址（回显）
    @Override
    public Address findById(Long id) {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getId,id);

        return addressMapper.selectOne(wrapper);
    }

    @Override
    public void updateAddress(Address address) {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        //1.设置修改set内容
        address.setConsignee(address.getConsignee());
        address.setSex(address.getSex());
        address.setPhone(address.getPhone());
        address.setDetail(address.getDetail());
        address.setLabel(address.getLabel());
        //2.设置where条件
        wrapper.eq(Address::getId,address.getId());
        //3.执行update
        addressMapper.update(address,wrapper);


    }
    //删除收货地址
    @Override
    public void deleteByIds(List<Long> ids) {
        if (CollectionUtil.isNotEmpty(ids)) {
            addressMapper.deleteBatchIds(ids);
        }
    }

    //设置默认地址
    @Override
    public void setDefault(Address address) {
      //1.把所有的userid的default修改为0
        //设置set之后修改default
        address.setIsDefault(0);
        address.setUpdateTime(new Date());
        address.setUpdateUser(UserHolder.get().getId());
        //设置where条件
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId,UserHolder.get().getId());
        addressMapper.update(address,wrapper);
      //2.指定的收货地址id的default修改为1
       address.setIsDefault(1);
        address.setUpdateTime(new Date());
        address.setUpdateUser(UserHolder.get().getId());
        LambdaQueryWrapper<Address> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Address::getId,address.getId());
         addressMapper.update(address,wrapper1);

    }

    @Override
    public Address getDefault() {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId,UserHolder.get().getId())
                .eq(Address::getIsDefault,1);//1 默认地址
        return addressMapper.selectOne(wrapper);
    }
}
